$(function () {

    listLive(1);
    if(!isOpen &&vlasProviderMap.type!="CC"){
        var context = '<div class="text-center pt10 mt20 pl20 pr20">' +
            '<p><span class="fs16">平台检查到您还未开通268云视频服务</br>是否要开通?</span></p>'+
            '</div>';
        parent.layer.confirm(
            context,
            {
                type: 1,
                title:'友情提示：',
                area: ['380px'],
                offset: 'auto',
                closeBtn:0,
                btn:['去开通'],
                btnAlign : 'c'
            },
            function (index) {
                parent.layer.close(index);
                getRedirectMenu(baseloaction+"/admin/course/serviceConfig",false)
            });
    }

    if(!isOpen &&vlasProviderMap.type=="CC") {
        var context = '<div class="text-center pt10 mt20 pl20 pr20">' +
            '<p><span class="fs16">功能暂不可用</span></p>'+
            '</div>';
        parent.layer.confirm(
            context,
            {
                type: 1,
                title:'友情提示：',
                area: ['380px'],
                offset: 'auto',
                closeBtn:0,
                btn:['知道了'],
                btnAlign : 'c'
            },
            function (index) {
                parent.layer.close(index);
                getRedirectMenu(baseloaction+"/admin/course/serviceConfig",false)
            });
    }
    var clipboard = new Clipboard('#clone');

    clipboard.on('error', function () {
        icModale(0, '复制链接失败');
    });
    clipboard.on('success', function() {
        icModale(0, '复制链接成功');
    });

    $('body').on('click', 'i.fa-clone', function () {
            var $parent = $(this).parents('div');
            var anchor = $parent.data('anchor');
            var sign = $parent.data('sign');
            var room = $parent.data('room');
            $('#anchor').text('https://www.96koo.net/common/live/Login.php?account=' + room + '&role=anchor&password=' + anchor + '&type=1&sign=' + sign);
            $('#assistant').text('https://www.96koo.net/common/live/Login.php?account=' + room + '&role=admin&type=2&sign=' + sign);
            $('#limit').text('https://www.96koo.net/common/live/Login.php?account=' + room + '&role=user&type=0&sign=' + sign);
            $('#free').text('https://www.96koo.net/common/live/loginf.php?account=' + room + '&role=user&freepassword=1&type=0&sign=' + sign);
            $('#anchor, #assistant, #limit, #free').prop('readonly', true);
            parent.layer.open({
                type: 1,
                area: ['800px', '400px'],
                skin: 'layui-layer-demo',
                title: '<strong>复制链接</strong>',
                content: $('#copy-url-tips').html(),
                btn: ['复制', '取消'],
                shadeClose: true,
                yes: function (index, layero) {
                    clipboard.text = function() {
                        return layero.find('div[id^=tab].active textarea').val();
                    };
                    $('#clone').click();
                },
                btn2: function () {
                    parent.layer.closeAll()
                }
            })
        })
        .on('click', '#btn1, #btn2', function () {
            listLive(1);
        })
        .on('click', '#live-list-context i.fa-trash', function () {
            var parent = $(this).parents('div:eq(0)');
            icModale(2, '确认删除选中直播吗?</br>删除后无法恢复！！！', '', function () {
                deleteLive(parent, parent.data('room'));
            })
        })


    function deleteLive(obj, room) {
        $.ajax({
            url: baseloaction + '/admin/course/ajaxLiveClose',
            data: {'room_id': room,"type":$(obj).data('service-type')},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    obj.parent('div').parent('div').parent('div').remove();
                    icModale(0, response.message);
                } else {
                    icModale(0, response.message);
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        })
    }

    function listLive(currentPage) {
        var params = $("#search-form").serializeJSON();
        params.type =$("input[name='type']").val();
        params = JSON.stringify(params);
        ajaxGetListData({
            url: baseloaction + '/admin/course/ajaxLiveList',//请求URL
            contextId: 'live-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: currentPage,//页码
            pageCall: listLive, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-course', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").text('（共'+total+'个直播）');
            }
        });
    }

});